<?php

namespace app\common\service;
// use app\common\service\PurseService as Purse;
use think\Db;

class UserService{
	/*
	用户注册
	*/
	public static function addUser($data){
		Db::startTrans();
		//用户表
		$uid = model('UserModel')->addNewUser($data);
		
		//助记词
		// $words = self::getWord();
		
		//生成代币钱包地址
		$coin     = model('CoinModel')->getCoinInfo(10);
		$address  = WalletService::createAddress($uid,$coin);

		//生成eth钱包地址 和 trx钱包地址
		 
		// $coin     = model('CoinModel')->getCoinInfo(1);
		// $address  = WalletService::createAddress($uid,$coin);

		// $coin     = model('CoinModel')->getCoinInfo(3);
		// $address  = WalletService::createAddress($uid,$coin);

		// $res2 = model('UserRecoveryModel')->setWords($uid,$words,$data['password'],$address);
		$res2 = 1;
		//注册赠送
		$config = model('RegConfigModel')->getRegConfig();
		if($config['is_open'] == 1 && $config['amount']>0){
			$res3 = BalanceService::updateBalance($uid,$config['wid'],$config['amount'],'注册赠送',13);
		} else {
			$res3 = 1;
		}

		//设置token
		$token  = model('TokenModel')->setToken($uid);

		//记录团队结构
		$depth = 1;
		$pid   = $data['pid'];
		while (true) {
			if ($pid == 0){
				break;
			} else {
				$res = Db::table('tp_user_team')->insert([
					'uid'=>$pid,
					'depth'=>$depth,
					'x_uid'=>$uid,
					'status'=>1,
				]);
				if(!$res){
					Db::rollback();
					return false;					
				}
				$pid = Db::table('tp_user')->where('id',$pid)->value('pid');
				$depth += 1;
			}
		}

		//设置业绩统计记录
		// $res4 = Db::table('tp_invest_statistics')->insert(['uid'=>$uid]);
		$res4 = 1;
		//赠送一个
		$config = Db::table('tp_invest_config')->where('id',1)->find();
		$res4 = Db::table('tp_invest_record')->insertGetId([
            'orderno'=>date('YmdHis'),
            'name'=>$config['name'],
            'uid'=>$uid,
            'wid'=>1,
            'cost'=>0,
            'price'=>$config['price'],
            'days'=>$config['days'],
            'rate'=>$config['rate'],
            'config_id'=>$config['id'],
            'order_date'=>date('Y-m-d H:i:s'),
            'open_date'=>date('Y-m-d H:i:s'),
            'status'=>2,
            'type'=>1,
        ]);
		if($uid && $res2 && $res3 && $token && $address && $res4){
			Db::commit();
			return ['uid'=>$uid,'token'=>$token,'username'=>$data['username']];
			// return ['uid'=>$uid,'words'=>$words,'token'=>$token,'address'=>$address,'username'=>$data['username']];
		} else {
			Db::rollback();
			return false;
		}
	}

	public static function getWord(){
		$word = model('MnemonicWordModel')->getWord();
		$words = implode(",", $word);
		$res = model('UserRecoveryModel')->checkWords($words);
		if($res){
			self::getWord();
		} else{
			return $words;
		}
	}


	//双轨
	private function dualTrack($pid,$position=1){
		if ($position == 1) {
			$jdid = $pid;
			while (true) {
				$next_jdid = Db::table('tp_user')->where('jdid',$jdid)->where('position',1)->value('id');
				if (empty($next_jdid)) {
					break;
				} else {
					$jdid = $next_jdid;
				}
			}
		} else {
			//左区优先制度。左区是否有人
			$left = Db::table('tp_user')->where('jdid',$pid)->where('position',1)->value('id');
			if (empty($left)) {
				$this->retData(1, '上级左区邀请人为空');
			} else {
				//右区账户左滑落
				$right = Db::table('tp_user')->where('jdid',$pid)->where('position',2)->value('id');
				if (empty($right)) {
					$jdid = $pid;
				} else {
					$jdid = $right;
					while (true) {
						$next_jdid = Db::table('tp_user')->where('jdid',$jdid)->where('position',1)->value('id');
						if (empty($next_jdid)) {
							break;
						} else {
							$jdid = $next_jdid;
						}
					}
				}
			}
		}
	}
}
